package com.androidlongs.androidpoptimeandoptionlibrary.common;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.widget.*;

import com.androidlongs.androidpoptimeandoptionlibrary.dao.*;
import com.androidlongs.androidpoptimeandoptionlibrary.listener.*;
import com.androidlongs.androidpoptimeandoptionlibrary.model.*;
import com.androidlongs.androidpoptimeandoptionlibrary.view.*;

import java.util.*;

/**
 * Created by androidlongs on 17/7/27.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

/**
 * 地区选择器依赖库
 * 使用前使用 初始化操作方法  将城市数据拷贝到手机中
 * 然后再调用显示的方法
 */
public class CommonOptionsPop {
    private OptionsPickerView mPvOptions;

    private CommonOptionsPop (){

    }
    private static  class  SingleCommonTimePop{
        private static CommonOptionsPop sCommonTimePop = new CommonOptionsPop();
    }

    public static CommonOptionsPop getinstance(){
        return SingleCommonTimePop.sCommonTimePop;
    }


    private Context mContext;

    /**
     * 在使用本库前 先要将城市相关数据拷贝到手机中
     */
    public void initFunction(Context context,String pageName){
        mContext = context;
        //导入数据库
        PopOptionDBManager dbHelper = new PopOptionDBManager(mContext,pageName);
        dbHelper.openDatabase();
    }
    private static ArrayList<RegionInfo> item1;

    private static ArrayList<ArrayList<RegionInfo>> item2 = new ArrayList<ArrayList<RegionInfo>>();

    private static ArrayList<ArrayList<ArrayList<RegionInfo>>> item3 = new ArrayList<ArrayList<ArrayList<RegionInfo>>>();

    private Handler handler = new Handler(Looper.getMainLooper()) {
        public void handleMessage (android.os.Message msg) {

            // 三级联动效果
            mPvOptions.setPicker(item1, item2, item3, true);
            mPvOptions.setCyclic(true, true, true);
            mPvOptions.setSelectOptions(0, 0, 0);
            log("设置显示.. .. ");
            mPvOptions.setCityOptionShow();

        }

    };
    private CommonOptionsSelectListener mCommonOptionsSelectListener;
    public void showTimeViewFunction(CommonOptionsSelectListener listener){

        this.mCommonOptionsSelectListener = listener;
       if (mContext instanceof Activity){
           showTimeViewFunction();
       }else{
           Toast.makeText(mContext, "context not activity instanceof ", Toast.LENGTH_SHORT).show();
       }

    }
    private void showTimeViewFunction(){
        // 创建地区选择器
        mPvOptions = new OptionsPickerView(mContext);
        //加载数据
        new Thread(new Runnable() {

            @Override
            public void run () {
                // TODO Auto-generated method stub
                log("读取数据");
                if (item1 != null && item2 != null && item3 != null) {
                    handler.sendEmptyMessage(0x123);
                    return;
                }
                item1 = (ArrayList<RegionInfo>) PopRegionDao.getProvencesOrCity(1);
                for (RegionInfo regionInfo : item1) {
                    item2.add((ArrayList<RegionInfo>) PopRegionDao.getProvencesOrCityOnParent(regionInfo.getId()));
                }

                for (ArrayList<RegionInfo> arrayList : item2) {
                    ArrayList<ArrayList<RegionInfo>> list2 = new ArrayList<ArrayList<RegionInfo>>();
                    for (RegionInfo regionInfo : arrayList) {


                        ArrayList<RegionInfo> q = (ArrayList<RegionInfo>) PopRegionDao.getProvencesOrCityOnParent(regionInfo.getId());
                        list2.add(q);

                    }
                    item3.add(list2);
                }
                log("读取数据完成");
                log("读取数据完龙 ");
                handler.sendEmptyMessage(0x123);

            }
        }).start();

        // 设置选择的三级单位
        // pwOptions.setLabels("省", "市", "区");
        mPvOptions.setTitle("选择城市");

        // 设置默认选中的三级项目
        // 监听确定选择按钮

        mPvOptions.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect (int options1, int option2, int options3) {
                // 返回的分别是三个级别的选中位置
                String tx = item1.get(options1).getPickerViewText() + item2.get(options1).get(option2).getPickerViewText() + item3.get(options1).get(option2).get(options3).getPickerViewText();
                log("选择的city is " + tx);

                String city = item1.get(options1).getPickerViewText();
                String province = item2.get(options1).get(option2).getPickerViewText();
                String county = item3.get(options1).get(option2).get(options3).getPickerViewText();

                if (mCommonOptionsSelectListener != null) {
                    mCommonOptionsSelectListener.onSelectOption(city,province,county);
                }
            }
        });

        mPvOptions.show();
    }



    private void log(String msg){
        Log.e("time_pop","|——————————————————————————————————————start——————————————————————————————————————————————————");
        Log.d("time_pop","|      "+msg);
        Log.e("time_pop","|——————————————————————————————————————end——————————————————————————————————————————————————");

    }
}
